package com.atguigu.realtime.join;

import com.atguigu.realtime.app.BaseSqlApp;
import com.atguigu.realtime.util.SQLUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2023/3/13 10:32
 */
public class RegularJoin_Inter extends BaseSqlApp {
    public static void main(String[] args) {
        new RegularJoin_Inter().init(
            4000,
            2,
            "RegularJoin_Inter"
        );
        
    }
    
    @Override
    public void handle(StreamExecutionEnvironment env,
                       StreamTableEnvironment tEnv) {
        // 每条数据, 在内存中保存 20s
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(20));
        
        tEnv.executeSql("create table t1(" +
                            " id int, " +
                            " name string " +
                            ")" + SQLUtil.getKafkaSourceDDL("t1", "RegularJoin_Inter", "csv"));
    
        tEnv.executeSql("create table t2(" +
                            " id int, " +
                            " age int " +
                            ")" + SQLUtil.getKafkaSourceDDL("t2", "RegularJoin_Inter", "csv"));
        
        
        // 内连接:
        // select .. from a join b on a.id=b.id
        // select .. from a, b where  a.id=b.id
        /*tEnv.sqlQuery("select " +
                          " t1.id, " +
                          " name, " +
                          " age " +
                          "from t1 " +
                          "join t2 on t1.id=t2.id")
            .execute()
            .print();*/
    
        tEnv.sqlQuery("select " +
                          " t1.id, " +
                          " name, " +
                          " age " +
                          "from t1 " +
                          ",t2 where t1.id=t2.id")
            .execute()
            .print();
        
        
    }
}
/*
常规 join:
    默认情况下, 所有数据会永久存储到状态中
    
    所以, 需要根据业务, 给数据设置 ttl
    
    比如: 下单
        order_info   order_detail
            1           1
            1           2
 */